package com.zwz.service.impl;

import com.zwz.dto.DishonestPersonDTO;
import com.zwz.entity.DishonestPerson;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.RiskInfoMain;
import com.zwz.mapper.DishonestPersonMapper;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.service.DishonestPersonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DishonestPersonServiceImpl implements DishonestPersonService {

    @Autowired
    private RiskInfoMainMapper mainMapper;

    @Autowired
    private DishonestPersonMapper dishonestMapper;

    @Override
    public DishonestPersonDTO createWithMain( String enterpriseId ) {
        // 创建主表记录
        // 创建主表记录
        // 主表操作
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(enterpriseId));


        // 设置子表关联
        DishonestPerson dishonest = new DishonestPerson();
        int k=0;
        dishonest.setId(k++);
        dishonest.setCaseNumber("1");
        //最后关联上
        dishonest.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));
        // 业务校验
        validateDishonest(dishonest);

        // 插入子表
        dishonestMapper.insert(dishonest);





        return new DishonestPersonDTO(enterpriseInfo, dishonest);
    }

    private RiskInfoMain createMainRecord(Integer companyId) {
        RiskInfoMain main = new RiskInfoMain();
        main.setCompanyId(companyId);
        mainMapper.insert(main);
        return main;
    }

    private void validateDishonest(DishonestPerson dishonest) {
        if (Boolean.TRUE.equals(dishonest.getCurrentDishonest())) {
            if (dishonest.getDishonestFilingDate() == null) {
                throw new IllegalArgumentException("当前失信必须包含立案日期");
            }
            if (StringUtils.isBlank(dishonest.getCaseNumber())) {
                throw new IllegalArgumentException("当前失信必须填写案号");
            }
        }
    }
}